#include "textmenu.h"
#include "delay.h"
#include "led.h"
#include "key.h"
#include "stdio.h"

u8g2_t u8g2;
textMenu_t textMenu;

static void blink(pTextMenu_t pTextMenu);
static void ledoff(pTextMenu_t pTextMenu);
static void ledon(pTextMenu_t pTextMenu);
static void backLightBrightness(pTextMenu_t pTextMenu);
static void Test(pTextMenu_t pTextMenu);

const char *menuItems[MAX_MENU] = {
    "1.Blink",
    "2.LED OFF",
    "3.LED ON",
    "4.Brightness",
    "5.Test",
};

void (*pSelectFuncCB[MAX_MENU])(pTextMenu_t pTextMenu) = {
    blink,
    ledoff,
    ledon,
    backLightBrightness,
    Test,
};

static unsigned char textMenu_KeyDownCheck(void)
{
    return Key_Scan(KEY_DOWN_PORT, KEY_DOWN_PIN);
}
static unsigned char textMenu_KeyDownCallback(void)
{
    textMenu.curMenuID++;
    if (textMenu.curMenuID >= (MAX_MENU - 1)) {
        textMenu.curMenuID = (MAX_MENU - 1);
    }

    return 1;
}

static unsigned char textMenu_KeyUpCheck(void)
{
    return Key_Scan(KEY_UP_PORT, KEY_UP_PIN);
}
static unsigned char textMenu_KeyUpCallback(void)
{
    textMenu.curMenuID--;
    if (textMenu.curMenuID < 0) {
        textMenu.curMenuID = 0;
    }

    return 1;
}

static unsigned char textMenu_KeyLeftCheck(void)
{
    return Key_Scan(KEY_LEFT_PORT, KEY_LEFT_PIN);
}
static unsigned char textMenu_KeyLeftCallback(void)
{
    return 1;
}

static unsigned char textMenu_KeyRightCheck(void)
{
    return Key_Scan(KEY_RIGHT_PORT, KEY_RIGHT_PIN);
}
static unsigned char textMenu_KeyRightCallback(void)
{
    return 1;
}

static unsigned char textMenu_KeySelectCheck(void)
{
    return Key_Scan(KEY_SELECT_PORT, KEY_SELECT_PIN);
}

static void blink(pTextMenu_t pTextMenu)
{
    while (1) {
        LED1_TOGGLE;
        delay_ms(500);

        if (pTextMenu->KeyRightCheck()) {
            break;
        }
    }
}

static void ledoff(pTextMenu_t pTextMenu)
{
    LED1_OFF;
    delay_ms(500);
}

static void ledon(pTextMenu_t pTextMenu)
{
    LED1_ON;
    delay_ms(500);
}

static void backLightBrightness(pTextMenu_t pTextMenu)
{
    unsigned char brightness = 100;
    char strOut[50] = {0};

    u8g2_ClearBuffer(&u8g2);
    while (1) {
        if (textMenu.KeyDownCheck()) {
            brightness -= 5;
            if (brightness < 0)
                brightness = 0;
            delay_ms(200);
        }

        if (textMenu.KeyUpCheck()) {
            brightness += 5;
            if (brightness > 100)
                brightness = 100;
            delay_ms(200);
        }
        
        OLED_ScreenSetBrightnessPercentage(brightness);
        sprintf(strOut, "Brightness: %d", brightness);
        u8g2_DrawStr(&u8g2, 15, 35, (const char *)strOut);
        u8g2_SendBuffer(&u8g2);
        u8g2_ClearBuffer(&u8g2);

        if (pTextMenu->KeyRightCheck()) {
            break;
        }
    }
    delay_ms(1000);
}

static void Test(pTextMenu_t pTextMenu)
{
    return;
}

void textMenu_Init(pTextMenu_t pTextMenu)
{
    unsigned char menuItem_index = 0;

    for (menuItem_index = 0; menuItem_index < MAX_MENU; menuItem_index++) {
        pTextMenu->pTmenuList[menuItem_index] = menuItems[menuItem_index];
    }

    pTextMenu->textMenuSize     = (sizeof(menuItems) / sizeof(menuItems[0]));
    pTextMenu->curMenuID        = 0;
    pTextMenu->KeyDownCheck     = textMenu_KeyDownCheck;
    pTextMenu->KeyDownCallback  = textMenu_KeyDownCallback;
    pTextMenu->KeyUpCheck       = textMenu_KeyUpCheck;
    pTextMenu->KeyUpCallback    = textMenu_KeyUpCallback;
    pTextMenu->KeyLeftCheck     = textMenu_KeyLeftCheck;
    pTextMenu->KeyLeftCallback  = textMenu_KeyLeftCallback;
    pTextMenu->KeyRightCheck    = textMenu_KeyRightCheck;
    pTextMenu->KeyRightCallback = textMenu_KeyRightCallback;
    pTextMenu->KeySelectCheck   = textMenu_KeySelectCheck;

    for (menuItem_index = 0; menuItem_index < MAX_MENU; menuItem_index++) {
        pTextMenu->KeySelectCallback[menuItem_index] = pSelectFuncCB[menuItem_index];
    }
}


void textMenu_Handle(pTextMenu_t pTextMenu)
{
    unsigned char tMenu_index = 0;

    if (pTextMenu->KeyDownCheck()) {
        pTextMenu->KeyDownCallback();
    }

    if (pTextMenu->KeyUpCheck()) {
        pTextMenu->KeyUpCallback();
    }

    if (pTextMenu->KeySelectCheck()) {
        pTextMenu->KeySelectCallback[pTextMenu->curMenuID](pTextMenu);
    }

    for (tMenu_index = 0; tMenu_index <= MAX_MENU; tMenu_index++) {
        if (textMenu.curMenuID < MENUS_IN_ONEPAGE) {
            if (textMenu.curMenuID == tMenu_index) {
                u8g2_DrawRFrame(&u8g2, 0, tMenu_index * TEXT_HEIGHT, LCD_WIDTH, BOX_HEIGHT, 2);
                u8g2_SetFont(&u8g2, u8g2_font_ncenB08_tr);
            }
            u8g2_DrawStr(&u8g2, TEXT_WIDTH_OFFSET, TEXT_HEIGHT * tMenu_index + 11, pTextMenu->pTmenuList[tMenu_index]);
        } else {
            if (textMenu.curMenuID == tMenu_index) {
                /* make sure new page menu text alway show at the last */
                u8g2_DrawRFrame(&u8g2, 0, (tMenu_index - (1 + textMenu.curMenuID - MENUS_IN_ONEPAGE)) * TEXT_HEIGHT, LCD_WIDTH,
                                BOX_HEIGHT, 2);
            }
            u8g2_DrawStr(&u8g2, TEXT_WIDTH_OFFSET, (tMenu_index - (1 + textMenu.curMenuID - MENUS_IN_ONEPAGE)) * TEXT_HEIGHT + 11,
                         pTextMenu->pTmenuList[tMenu_index]);
        }
    }

    u8g2_SendBuffer(&u8g2);
    u8g2_ClearBuffer(&u8g2);
}


